<?php
/**
 * UserController
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */

namespace Api\Controller;

use Admin\Service\UserService;
use Model\LiveCateogyModel;
use Model\UserFavoriteModel;
use Model\UserHistoryModel;
use Model\UserModel;
use Think\Controller;
use Model\RoomModel;

class UserController extends BaseController
{

    /**
     * 获取用户关注列表
     * @http-method GET
     */
    public function favorite()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $userId = I('get.userId');
        if(empty($userId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userFavoriteModel = new UserFavoriteModel();
        $userFavoriteList = $userFavoriteModel->where('userId = '.$userId)->select();
        $roomModel = new RoomModel();
        $categoryModel = new LiveCateogyModel();
        $userModel = new UserModel();
        foreach($userFavoriteList as $k => $v){
            $roomInfo = $roomModel->get($v['roomId']);
            $userFavoriteList[$k]['roomName'] = $roomInfo['name'];
            $userFavoriteList[$k]['roomNumber'] = $roomInfo['roomNumber'];
            $userFavoriteList[$k]['roomImage'] = $_SERVER["SERVER_NAME"].$roomInfo['image'];
            $userFavoriteList[$k]['roomDescription'] = $roomInfo['description'];
            $userFavoriteList[$k]['roomStatus'] = $roomInfo['status'];
            $userFavoriteList[$k]['categoryId'] = $roomInfo['categoryId'];
            $categoryInfo = $categoryModel->get($roomInfo['categoryId']);
            $userFavoriteList[$k]['categoryName'] = $categoryInfo['name'];
            $userInfo = $userModel->get($v['userId']);
            $userFavoriteList[$k]['nickname'] = $userInfo['nickname'];
        }

        if($userFavoriteList){
            $result['code'] = 200;
            $result['msg'] = $userFavoriteList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }
        $this->ajaxReturn($result, 'JSON');
    }

    /**
     * 添加用户关注
     * @http-method POST
     */
    public function addFavorite()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $userId = I('post.userId');
        $roomId = I('post.roomId');
        $createTime = date('Y-m-d H:i;s');

        if(empty($userId) || empty($roomId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userFavoriteModel = new UserFavoriteModel();
        $userFavoriteAdd = $userFavoriteModel->add([
            'userId' => $userId,
            'roomId' => $roomId,
            'createTime' => $createTime
        ]);

        if($userFavoriteAdd){
            $result['code'] = 200;
            $result['msg'] = 'ok';
        }else{
            $result['code'] = 404;
            $result['msg'] = '添加失败';
        }
        $this->ajaxReturn($result, 'JSON');
    }

    /**
     * 删除用户关注
     * @http-method GET
     */
    public function delFavorite()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $favoriteId = I('get.id');

        if(empty($favoriteId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userFavoriteModel = new UserFavoriteModel();
        $userFavoriteDel = $userFavoriteModel->delete($favoriteId);

        if($userFavoriteDel){
            $result['code'] = 200;
            $result['msg'] = 'ok';
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }
        $this->ajaxReturn($result, 'JSON');
    }

    /**
     * 获取用户观看直播历史列表
     * @http-method GET
     */
    public function history()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $userId = I('get.userId');
        if(empty($userId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userHistoryModel = new UserHistoryModel();
        $userHistoryList = $userHistoryModel->where('userId = '.$userId)->select();
        $roomModel = new RoomModel();
        foreach($userHistoryList as $k => $v){
            $roomInfo = $roomModel->get($v['roomId']);
            $userHistoryList[$k]['roomName'] = $roomInfo['name'];
            $userHistoryList[$k]['roomNumber'] = $roomInfo['roomNumber'];
            $userHistoryList[$k]['roomImage'] = $_SERVER["SERVER_NAME"].$roomInfo['image'];
            $userHistoryList[$k]['roomDescription'] = $roomInfo['description'];
            $userHistoryList[$k]['roomStatus'] = $roomInfo['status'];
        }

        if($userHistoryList){
            $result['code'] = 200;
            $result['msg'] = $userHistoryList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }
        $this->ajaxReturn($result, 'JSON');

    }

    /**
     * 添加用户观看直播历史记录
     * @http-method POST
     */
    public function addHistory()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $userId = I('post.userId');
        $roomId = I('post.roomId');
        $createTime = date('Y-m-d H:i;s');

        if(empty($userId) || empty($roomId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userHistoryModel = new UserHistoryModel();
        $userHistoryCount = $userHistoryModel->countByFilter(['userId' => $userId, 'roomId' => $roomId]);

        if($userHistoryCount == 0){
            $userHistoryAdd = $userHistoryModel->add([
                'userId' => $userId,
                'roomId' => $roomId,
                'updateTime' => $createTime,
                'createTime' => $createTime
            ]);

            if($userHistoryAdd){
                $result['code'] = 200;
                $result['msg'] = 'ok';
            }else{
                $result['code'] = 404;
                $result['msg'] = '添加失败';
            }
        }else{
            $userHistoryResult = $userHistoryModel->where('userId = '.$userId.' AND roomId = '.$roomId)->setField('updateTime', $createTime);

            if($userHistoryResult){
                $result['code'] = 200;
                $result['msg'] = 'ok';
            }else{
                $result['code'] = 404;
                $result['msg'] = '添加失败';
            }
        }
        $this->ajaxReturn($result, 'JSON');
    }


    /**
     * 用户登录
     * @http-method GET
     */
    public function login()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $from = I('post.from');
        $createTime = date('Y-m-d H:i:s');

        if($from == ''){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        /*
         * $from
         * 0 => pc
         * 1 => qq
         * 2 => weixin
         * */
        $userModel = new UserModel();
        $userService = new UserService();
        if($from == 0){
            $username = I('post.username');
            $password = I('post.password');
            if(empty($username) || empty($password)){
                $result['code'] = 404;
                $result['msg'] = '参数异常';

                $this->ajaxReturn($result, 'JSON');
            }
            $userResult = $userService->validate($username, md5($password));
        }else{
            $picture = urldecode(I('post.picture'));
            $nickname = urldecode(I('post.nickname'));
            $openId = urldecode(I('post.openId'));
            if(empty($picture) || empty($nickname) || empty($openId)){
                $result['code'] = 404;
                $result['msg'] = 'picture, nickname, openId, 参数异常';
                $result['picture'] = $picture;
                $result['nickname'] = $nickname;
                $result['openId'] = $openId;


                $this->ajaxReturn($result, 'JSON');
            }

            $userResult = $userService->isExist($openId);
            if(empty($userResult)){
                //如果没有该用户，则增加
                $userResult = $userModel->add([
                    'username' => $openId,
                    'password' => $openId,
                    'nickname' => $nickname,
                    'picture' => $picture,
                    'from' => $from,
                    'groupId' => 0,
                    'createTime' => $createTime
                ]);
            }
        }

        if(empty($userResult)){
            $result['code'] = 404;
            $result['msg'] = '用户名密码错误';

            $this->ajaxReturn($result, 'JSON');
        }

        $userInfo = $userModel->get($userResult);
        if($userInfo['status'] == 0){
            $result['code'] = 404;
            $result['msg'] = '用户已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }

        $data['lastIp'] = get_client_ip();
        $data['lastTime'] = $createTime;
        $userModel->where('id = '.$userResult)->save($data);


        $result['code'] = 200;
        $result['msg']['userId'] = $userResult;
        $this->ajaxReturn($result, 'JSON');
    }


    /**
     * 获取指定用户信息
     * @http-method GET
     */
    public function getUserInfo()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $userId = I('get.id');

        if(empty($userId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $userModel = new UserModel();
        $userInfo = $userModel->field('
        id,
        username,
        permission,
        from,
        nickname,
        picture,
        sex,
        phone,
        email,
        balanceMoney,
        fordAvailable,
        fordTotal,
        status,
        lastIp,
        lastTime,
        createTime')->get($userId);

        if($userInfo){
            if($userInfo['from'] == 0){
                $userInfo['picture'] = $_SERVER["SERVER_NAME"].$userInfo['picture'];
            }
            $result['code'] = 200;
            $result['msg'] = $userInfo;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }
        $this->ajaxReturn($result, 'JSON');
    }
} 